package com.jinke.api.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinke.api.modules.app.enums.ChannelStatus;
import com.jinke.api.modules.ums.mapper.ChannelCarrierMapper;
import com.jinke.api.modules.ums.mapper.ChannelMapper;
import com.jinke.api.modules.ums.model.Channel;
import com.jinke.api.modules.ums.model.ChannelCarrier;
import com.jinke.api.modules.ums.model.ChannelRoot;
import com.jinke.api.modules.ums.model.UserChannel;
import com.jinke.api.modules.ums.service.ChannelCarrierService;
import com.jinke.api.modules.ums.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 承运商与货运商关系表Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class ChannelCarrierServiceImpl extends ServiceImpl<ChannelCarrierMapper, ChannelCarrier> implements ChannelCarrierService {


    @Autowired
    private ChannelCarrierService channelCarrierService;
    /**
     * 获取所有的渠道 货运商
     *
     * @return
     */
    @Override
    public Map<String, List<Integer>>  getAllChannels() {
        LambdaQueryWrapper<ChannelCarrier> channelCarrierLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ChannelCarrier> select = channelCarrierLambdaQueryWrapper
                //查出这两列
                .select(ChannelCarrier::getId, ChannelCarrier::getChannelCarrierName);

        List<ChannelCarrier> channels = list(select);
        //保存唯一的channelCarrierName，并把同样的channelCarrierName对应的id 收集起来和channelCarrierName一一对应
        Map<String, List<Integer>> result = channels.stream()
                .collect(Collectors.groupingBy(ChannelCarrier::getChannelCarrierName,
                        Collectors.mapping(ChannelCarrier::getId, Collectors.toList())));


        return result;
    }

    /**
     * 通过channel_carrier选择的三方平台和货运商名确定一个
     *
     * @param carrierName
     * @param platformId
     * @return
     */
    @Override
    public List<ChannelCarrier> getCarrierByCarrierNameAndPlatformId(String carrierName, Integer platformId) {
        LambdaQueryWrapper<ChannelCarrier> channelCarrierLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ChannelCarrier> select = channelCarrierLambdaQueryWrapper
                //查出ID
                .select(ChannelCarrier::getId)
                .eq(ChannelCarrier::getChannelCarrierName, carrierName)
                .eq(ChannelCarrier::getChannelPlatformId, platformId);


        return list(select);
    }
}
